package net.xxcl.zcgl.controller;

import net.xxcl.zcgl.pojo.Criteria;
import net.xxcl.zcgl.pojo.Dept;
import net.xxcl.zcgl.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: 夏汛
 * Date: 11-12-20
 * Time: 下午9:24
 */
@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 获取部门树
     * @return
     */
    @RequestMapping(value="/getDeptTree",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> getDeptTree(){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        criteria.setOrderByClause("id asc");
        Object t = deptService.getDeptsTree();
        responseMap.put("key",t);
        return responseMap;
    }

    /**
     * 获取部门分页列表
     * @param start
     * @param limit
     * @param queryParam
     * @return
     */
    @RequestMapping(value = "/depts")
    public @ResponseBody Map<String,Object> getKemus(@RequestParam(value = "start",required = false) Integer start,
                                                     @RequestParam(value = "limit",required = false) Integer limit,
                                                     @RequestParam(value = "queryParam",required = false) String queryParam){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        if(!(queryParam==null || queryParam=="")){
            criteria.put("name","%"+queryParam+"%");
        }
        int total = deptService.countByExample(criteria);
        if(start!=null) criteria.setMysqlOffset(start);
        if(start!=null && limit!=null) criteria.setMysqlLength(limit);
        List<Dept> list = deptService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 新增或编辑
     * @param dept
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> saveOrUpdate(@ModelAttribute("dept") Dept dept){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try{
            //新增部门
            if(dept.getId()==null){
                deptService.insert(dept);
                responseMap.put("method", "Create");
                responseMap.put("success", "true");
                responseMap.put("info", "新增成功！");
            }
            //编辑部门
            else {
                deptService.updateByPrimaryKey(dept);
                responseMap.put("success", "true");
                responseMap.put("info", "编辑成功！");
            }
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
        }
        return responseMap;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping(value="/remove",method=RequestMethod.POST)
    public @ResponseBody Map<String,Object> remove(@RequestParam("ids") String ids){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try {
            deptService.deleteDept(ids);
            responseMap.put("success", "true");
            responseMap.put("info", "删除成功！");
            return responseMap;
        }catch(Exception e) {
            e.printStackTrace();
            responseMap.put("info", e.getClass()+":"+e.getMessage());
            return responseMap;
        }
    }
}
